package com.haochang.chunkvod.controller.adapter.cash;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.haochang.base.BaseTextView;
import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.activity.cash.BuyOrderActivity;
import com.haochang.chunkvod.controller.activity.cash.CashHomePageActivity;
import com.haochang.chunkvod.controller.activity.cash.CheckOutActivity;
import com.haochang.chunkvod.controller.activity.cash.SetTimePreBuyActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.BaseInfo;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.PrintEnum;
import com.haochang.chunkvod.model.cash.PrintInfo;
import com.haochang.chunkvod.model.order.RoomInfo;
import com.haochang.chunkvod.tools.vod.other.DrawableUtils;
import com.haochang.chunkvod.tools.vod.thread.PrintThread;
import com.haochang.chunkvod.tools.vod.thread.ThreadPoolUtil;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

/**
 * 收银首页适配器
 */
public class CashHomePageAdapter extends QuickAdapter
{
	private Context mContext;
	private boolean isVehicle = false;
	private CashData cashData;
	private List<BaseInfo> baseInfoList;

	public CashHomePageAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
		cashData = new CashData((CashHomePageActivity) mContext);
	}

	public void setVehicle(boolean vehicle)
	{
		isVehicle = vehicle;
	}

	public void setBaseInfoList(List<BaseInfo> baseInfoList)
	{
		this.baseInfoList = baseInfoList;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final Object item)
	{
		final RoomInfo roomInfo = (RoomInfo) item;
		final BaseTextView tv_roomStatus = helper.getView(R.id.tv_roomStatus);
		helper.setText(R.id.tv_roomNum, roomInfo.getRoomNum());
		helper.setText(R.id.tv_roomType, roomInfo.getRoomType() + "(" + roomInfo.getRoomPeopleCount() + ")");
		helper.setText(R.id.tv_startTime, TextUtils.isEmpty(roomInfo.getStart_time()) ? "" : "进 " + TimeUtils.getMinutesString(roomInfo.getStart_time()));
		helper.setText(R.id.tv_endTime, TextUtils.isEmpty(roomInfo.getEnd_time()) ? "" : "结 " + TimeUtils.getMinutesString(roomInfo.getEnd_time()));
		if (roomInfo.getIsHonor() == 1)
			helper.getView(R.id.img_big).setVisibility(View.VISIBLE);
		else
			helper.getView(R.id.img_big).setVisibility(View.GONE);
		if (roomInfo.getType() == 0)
		{
			helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);//隐藏批次号
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.setDefaultRoomStyle(mContext, roomInfo.getRoomStatus(), roomInfo.getUsage(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setRoomStyleByService(mContext, roomInfo.getRoomStatus(), roomInfo.getUsage(), helper.getView(), tv_roomStatus, baseInfoList);
		} else
		{
			if (roomInfo.getAbs_number() == 0)
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.GONE);
			} else
			{
				helper.getView(R.id.tv_batch_num).setVisibility(View.VISIBLE);
				helper.setText(R.id.tv_batch_num, "(+" + roomInfo.getAbs_number() + ")");
			}
			if (CollectionUtils.isEmpty(baseInfoList))
				DrawableUtils.settDefaultDeskStyle(mContext, roomInfo.getRoomStatus(), helper.getView(), tv_roomStatus);
			else
				DrawableUtils.setDeskStyleByService(mContext, roomInfo.getRoomStatus(), helper.getView(), tv_roomStatus, baseInfoList);
		}
		helper.getView(R.id.list_layout).setTag(R.id.cash_home_page_list, roomInfo);
		helper.getView(R.id.list_layout).setOnClickListener(onBaseClickListener);
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.list_layout:
				final RoomInfo roomInfo = (RoomInfo) v.getTag(R.id.cash_home_page_list);
				final List<String> mMoreOptionItemList = new ArrayList<String>();
				if (isVehicle)
				{
					if (roomInfo.getBillStatus().equals("unpaied") || roomInfo.getBillStatus().equals("paied"))
						mMoreOptionItemList.add("买单");
					if (roomInfo.getBillStatus().equals("paying"))
					{
						mMoreOptionItemList.add("取消买单");
						mMoreOptionItemList.add("结账");
					}
					if (roomInfo.getBillStatus().equals("unpaied"))
						mMoreOptionItemList.add("收取预付款");
				}
				if (!CollectionUtils.isEmpty(mMoreOptionItemList))
					new VodDialog.Builder(mContext).dialogEnum(VodDialog.DialogEnum.LIST).ListTitleName(roomInfo.getRoomNum())
							.onDialogListListener(new VodDialog.Builder.OnDialogListListener()
							{
								@Override
								public void onSelectItem(int position)
								{
									if (position > 0)
									{
										if (mMoreOptionItemList.get(position - 1).equals("买单"))
										{
											onBuyOrderClick(roomInfo);
										} else if (mMoreOptionItemList.get(position - 1).equals("取消买单"))
										{
											onCancelPayClick(roomInfo.getBillId());
										} else if (mMoreOptionItemList.get(position - 1).equals("结账"))
										{
											onCheckOutClick(roomInfo);
										} else if (mMoreOptionItemList.get(position - 1).equals("收取预付款"))
										{

										}
									}
								}
							}).dialogList(mMoreOptionItemList).cancelable(true).build().show();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 点击买单
	 * @param roomInfo
	 */
	private void onBuyOrderClick(final RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, BuyOrderActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		if (roomInfo.getType() == 0)
			intent.putExtra(IntentKey.TABLE_BUY_ORDER, false);
		else
			intent.putExtra(IntentKey.TABLE_BUY_ORDER, true);
		((CashHomePageActivity) mContext).startActivity(intent);
	}

	/**
	 * 点击结账
	 */
	private void onCheckOutClick(final RoomInfo roomInfo)
	{
		cashData.requestCheckOutNum(roomInfo.getBillId(), "0", new CashData.ICheckOutNumListener()
		{
			@Override
			public void onRequestSuccess(JSONObject jsonObject)
			{
				Intent intent = new Intent(mContext, CheckOutActivity.class);
				intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
				intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
				intent.putExtra(IntentKey.IS_FORCE_CHECKOUT, "0");
				intent.putExtra(IntentKey.CHECKOUT_TYPE, PrintEnum.PRINT_CHECK_OUT);
				((CashHomePageActivity) mContext).startActivityForResult(intent, IntentCode.CHECKOUT_REQUEST);
			}

			@Override
			public void onRequestFail(int errno, String errMsg)
			{
				if (errno == 4009)
					new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE_CUSTOM_VIEW).btnNegativeText(R.string.cancel)
							.btnNeutralText(R.string.sure).cancelable(false).contentName("该房间正在被其他设备结账，无法在该设备上进行结账操作!")
							.onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
							{
								@Override
								public void onOnlyCancelClick()
								{
								}

								@Override
								public void onOkClick()
								{
									Intent intent = new Intent(mContext, CheckOutActivity.class);
									intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
									intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
									intent.putExtra(IntentKey.IS_FORCE_CHECKOUT, "1");
									intent.putExtra(IntentKey.CHECKOUT_TYPE, PrintEnum.PRINT_CHECK_OUT);
									mContext.startActivity(intent);
								}

								@Override
								public void onCancelClick()
								{
								}
							}).build().show();
			}
		});
	}

	/**
	 * 点击设时预买
	 */
	private void onSetTimePreBuyClick(RoomInfo roomInfo)
	{
		Intent intent = new Intent(mContext, SetTimePreBuyActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomInfo.getRoomNum());
		intent.putExtra(IntentKey.BILL_ID, roomInfo.getBillId());
		intent.putExtra(IntentKey.ROOM_TYPE_ID, roomInfo.getRoomTypeId());
		intent.putExtra(IntentKey.BILL_STATUS, roomInfo.getBillStatus());
		mContext.startActivity(intent);
	}

	/**
	 * 点击打印预结算
	 */
	private void onPrintPreCheckOutClick(String billId)
	{
		cashData.requestPreCheckOutInfo(billId, new CashData.IPrintInfoListener()
		{
			@Override
			public void onRequestSuccess(JSONObject jsonObject)
			{
				PrintInfo printInfo = new PrintInfo();
				printInfo.setType(PrintEnum.PRINT_PRE_CHECK_OUT.getName());
				printInfo.setContent(jsonObject.toString());
				ThreadPoolUtil.getInstanceSingleTaskExecutor().submit(new PrintThread(PrintEnum.PRINT_PRE_CHECK_OUT, printInfo));
			}

			@Override
			public void onRequestFail(int errno, String errMsg)
			{
				ToastUtils.showText(mContext, "获取预结列印信息失败");
			}
		});
	}

	/**
	 * 点击取消支付中
	 */
	private void onCancelPayClick(String billId)
	{
		cashData.requestBillStatusRollBack(billId, new CashData.IBillStatusRollBackListener()
		{
			@Override
			public void onRequestSuccess()
			{
				//成功之后重新拉取首页数据
				EventProxy.notifyEvent(IEventConstant.CANCEL_PAY);
			}
		});
	}

}
